package cn.lanqiao.chatroom;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class TestSockClient {
    public static void main(String[] args) {
        Socket socket = null;
        DataOutputStream dos = null;
//        DataInputStream dis = null;
        String str = null;
        Scanner input = new Scanner(System.in);
        try {
            socket = new Socket("localhost", 8888);
            DataInputStream dis = new DataInputStream(socket.getInputStream());
            dos = new DataOutputStream(socket.getOutputStream());

            //接受服务器信息 内部类  ()->{}
            new Thread(() -> {
                String s = null;
                try {
                    while ((s = dis.readUTF()) != null)
                        System.out.println(s);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();

            //发送信息内容
            do {
                System.out.println("我说：");
                str = input.next();
                dos.writeUTF(str);
            } while (!str.equals("88"));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
//                dis.close();
                dos.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
